/*
 * VHECRStandingsGrid.java
 * Created on 6 sept. 2009
 *
 * VirtualHockey Internet Platform
 * http://code.google.com/p/virtualhockey/
 *
 * Copyright (c) 2005-2009, Jens and Jan Janke (VirtualHockey Project)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/.
 */

package com.hockeo.vhweb.client.ui.widget;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.SortDir;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnData;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;
import com.extjs.gxt.ui.client.widget.grid.RowNumberer;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.google.gwt.i18n.client.NumberFormat;
import com.hockeo.vhweb.client.VHMain;
import com.hockeo.vhweb.client.res.VHResConstants;
import com.hockeo.vhweb.client.rpc.VHRpcResultReceiver;
import com.hockeo.vhweb.client.utils.VHJsData;

/**
 * Special grid that displays the Earth Nations Standings.
 *
 * @version $Id$
 * @author jjanke
 */
public class VHENRStandingsGrid extends LayoutContainer implements VHRpcResultReceiver<List<VHJsData>>
{
  private static final int          PAGE_SIZE = 20;

  private final ListStore<VHJsData> d_store;
  private Grid<VHJsData>            d_grid;

  /**
   * Builds a new ENR standings grid.
   */
  public VHENRStandingsGrid()
  {
    d_store = new ListStore<VHJsData>();

    setLayout( new FitLayout() );
    buildGrid();
    add( d_grid );
  }

  public void setRpcData( List<VHJsData> result, String strPurpose )
  {
    d_store.removeAll();
    d_store.add( result );
    d_store.sort( "rank", SortDir.ASC );
  }

  private void buildGrid()
  {
    final NumberFormat numberFormat = NumberFormat.getFormat( "0.00" );
    GridCellRenderer<VHJsData> renderer = new GridCellRenderer<VHJsData>() {
      public String render( VHJsData data, String strProperty, ColumnData config, int rowIndex, int colIndex,
          ListStore<VHJsData> store, Grid<VHJsData> grid )
      {
        if ( strProperty.equals( "rank" ) )
          return "<span style='font-weight: bold;'>" + data.getInt( strProperty ) + "</span>";
        else if ( strProperty.equals( "rankTendency" ) )
          return "<img src='images/picto/rank_" + data.getString( strProperty ).toLowerCase() + ".gif'/>";
        else if ( strProperty.equals( "association_id" ) )
          return "<img src='images/flags/16x16/" + data.getString( strProperty ) + ".png'/>";

        return String.valueOf( data.get( strProperty ) );
      }
    };

    // Column definitions
    //
    VHResConstants constants = VHMain.getConstants();
    ArrayList<ColumnConfig> listCols = new ArrayList<ColumnConfig>();
    ColumnConfig col;

    RowNumberer rowNumberer = new RowNumberer();
    listCols.add( rowNumberer );

    col = new ColumnConfig();
    col.setId( "rank" );
    col.setHeader( constants.rankAbbrev() );
    col.setToolTip( constants.rank() );
    col.setWidth( 28 );
    col.setRenderer( renderer );
    col.setAlignment( HorizontalAlignment.RIGHT );
    col.setResizable( false );
    listCols.add( col );

    col = new ColumnConfig();
    col.setId( "rankTendency" );
    col.setHeader( constants.rankTendencyAbbrev() );
    col.setToolTip( constants.rankTendency() );
    col.setWidth( 26 );
    col.setRenderer( renderer );
    col.setAlignment( HorizontalAlignment.CENTER );
    col.setResizable( false );
    listCols.add( col );

    col = new ColumnConfig();
    col.setId( "prevRank" );
    col.setHeader( constants.prevRankAbbrev() );
    col.setToolTip( constants.prevRank() );
    col.setWidth( 28 );
    col.setAlignment( HorizontalAlignment.RIGHT );
    col.setResizable( false );
    listCols.add( col );

    col = new ColumnConfig();
    col.setId( "rankDiff" );
    col.setHeader( constants.rankDiffAbbrev() );
    col.setToolTip( constants.rankDiff() );
    col.setWidth( 35 );
    col.setNumberFormat( NumberFormat.getFormat( "+#;-#" ) );
    col.setAlignment( HorizontalAlignment.RIGHT );
    col.setResizable( false );
    listCols.add( col );

    col = new ColumnConfig();
    col.setId( "association_id" );
    col.setHeader( constants.associationAbbrev() );
    col.setToolTip( constants.association() );
    col.setWidth( 26 );
    col.setRenderer( renderer );
    col.setAlignment( HorizontalAlignment.CENTER );
    col.setResizable( false );
    listCols.add( col );

    col = new ColumnConfig();
    col.setId( "association_name" );
    col.setHeader( constants.association() );
    col.setWidth( 200 );
    listCols.add( col );

    col = new ColumnConfig();
    col.setId( "pointsTotal" );
    col.setHeader( constants.pointsTotalAbbrev() );
    col.setToolTip( constants.pointsTotal() );
    col.setWidth( 40 );
    col.setNumberFormat( numberFormat );
    col.setAlignment( HorizontalAlignment.RIGHT );
    listCols.add( col );

    col = new ColumnConfig();
    col.setId( "pointsS1" );
    col.setHeader( constants.pointsS1Abbrev() );
    col.setToolTip( constants.pointsS1() );
    col.setWidth( 40 );
    col.setNumberFormat( numberFormat );
    col.setAlignment( HorizontalAlignment.RIGHT );
    listCols.add( col );

    col = new ColumnConfig();
    col.setId( "pointsS2" );
    col.setHeader( constants.pointsS2Abbrev() );
    col.setToolTip( constants.pointsS2() );
    col.setWidth( 40 );
    col.setNumberFormat( numberFormat );
    col.setAlignment( HorizontalAlignment.RIGHT );
    listCols.add( col );

    col = new ColumnConfig();
    col.setId( "pointsS3" );
    col.setHeader( constants.pointsS3Abbrev() );
    col.setToolTip( constants.pointsS3() );
    col.setWidth( 40 );
    col.setNumberFormat( numberFormat );
    col.setAlignment( HorizontalAlignment.RIGHT );
    listCols.add( col );

    col = new ColumnConfig();
    col.setId( "pointsS4" );
    col.setHeader( constants.pointsS4Abbrev() );
    col.setToolTip( constants.pointsS4() );
    col.setWidth( 40 );
    col.setNumberFormat( numberFormat );
    col.setAlignment( HorizontalAlignment.RIGHT );
    listCols.add( col );

    col = new ColumnConfig();
    col.setId( "pointsS5" );
    col.setHeader( constants.pointsS5Abbrev() );
    col.setToolTip( constants.pointsS5() );
    col.setWidth( 40 );
    col.setNumberFormat( numberFormat );
    col.setAlignment( HorizontalAlignment.RIGHT );
    listCols.add( col );

    col = new ColumnConfig();
    col.setId( "pointsExpiringNextSeason" );
    col.setHeader( constants.pointsExpiringAbbrev() );
    col.setToolTip( constants.pointsExpiring() );
    col.setWidth( 40 );
    col.setNumberFormat( numberFormat );
    col.setAlignment( HorizontalAlignment.RIGHT );
    listCols.add( col );

    col = new ColumnConfig();
    col.setId( "numTeamsS1" );
    col.setHeader( constants.numTeamsS1Abbrev() );
    col.setToolTip( constants.numTeamsS1() );
    col.setWidth( 40 );
    col.setAlignment( HorizontalAlignment.RIGHT );
    listCols.add( col );

    final ColumnModel cm = new ColumnModel( listCols );

    // Grid
    //
    d_grid = new Grid<VHJsData>( d_store, cm );
    d_grid.addPlugin( rowNumberer );
    d_grid.setStripeRows( true );
    d_grid.setBorders( true );
    d_grid.setAutoWidth( true );
  }
}
